import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {BaseService} from "../../../shared/service/base.service";
import {ApiUrl} from "../../../config/api-url";

@Component({
  selector: 'app-store-time-limit',
  templateUrl: './store-time-limit.component.html',
  styleUrls: ['./store-time-limit.component.less']
})
export class StoreTimeLimitComponent implements OnInit {

  @Input()
  nzVisible = false;
  @Input()
  store: any;
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  isOkLoading = false;
  businessStartTime: any;
  businessEndTime: any;
  constructor(private baseService: BaseService) { }

  ngOnInit(): void {

  }

  ngOnChanges() {
    //console.log(this.store)
    if (this.store?.businessStartTime != null && this.store?.businessEndTime != null) {
      this.businessStartTime = this.store.businessStartTime;
      this.businessEndTime = this.store.businessEndTime;
    }
  }

  handleCancel() {
    this.businessStartTime = null;
    this.businessEndTime = null;
    this.store = null;
    this.nzVisible = false;
    this.emit.emit(this.nzVisible);
  }

  handleOk() {
    if (this.businessStartTime == null) {
      this.baseService.layer.messageError('营业开始时间不能为空');
      return;
    }
    if (this.businessEndTime == null) {
      this.baseService.layer.messageError('营业结束时间不能为空');
      return;
    }
    this.isOkLoading = true;
    let body = {
      id: this.store.id,
      userId: this.store.userId,
      businessStartTime: this.businessStartTime,
      businessEndTime: this.businessEndTime,
    };
    this.baseService.put(ApiUrl.MerchantStoreUrl, body).subscribe(data => {
      if (data.status == 0) {
        this.baseService.layer.messageSuccess('修改成功');
        this.businessStartTime = null;
        this.businessEndTime = null;
        this.store = null;
        this.handleCancel();
        this.isOkLoading = false;
      } else {
        this.baseService.layer.messageError(data.msg);
        this.isOkLoading = false;
      }
    });
  }
}
